import { QUIView } from "./QUIView";
import QGamePlugin from "./QGamePlugin";
export type QUIHorizontalLayoutConfig = {
    id?: string;
    class?: string;
    widthRatio?: number;
    heightRatio?: number;
    width?: number;
    height?: number;
    miniWidth?: number;
    miniHeight?: number;
    maxWidth?: number;
    maxHeight?: number;
    left?: number;
    top?: number;
    background?: number | string;
    borderColor?: number | string;
    borderWidth?: number;
    aplpha?: number;
};

export class QUIHorizontalLayout extends QUIView {
    protected name: string = "QUIHorizontalLayout";

    constructor(
        scene: Phaser.Scene,
        manage: QGamePlugin,
        config?: QUIHorizontalLayoutConfig
    ) {
        super(scene, manage, config);
        if (config && config.aplpha) {
            this.Alpha = config.aplpha;
        } else {
            this.Alpha = 0;
        }
    }

    calcWidth(): number {
        let width = this.paddingLeft + this.paddingRight;
        for (let i = 0; i < this.childViews.length; i++) {
            const view = this.childViews[i];
            width += view.Width + this.gap;
        }
        if (this.childViews.length > 0) {
            width -= this.gap;
        }
        return width;
    }

    reLayout(): void {
        const expWidth = this.calcWidth();
        if (expWidth > this.width || this.alignX == "left") {
            let width = this.paddingLeft;
            for (let i = 0; i < this.childViews.length; i++) {
                const view = this.childViews[i];
                view.Top = 0;
                view.Left = width;
                if (view.Height > this.height) {
                    view.Height = this.height;
                }
                width += view.Width + this.gap;
            }
            if (this.childViews.length > 0) {
                width -= this.gap;
            }
            this.width = width + this.paddingRight;
            this.reSize();
        } else {
            if (this.alignX == "right") {
                let width = this.width - this.paddingRight;
                for (let i = this.childViews.length - 1; i >= 0; i--) {
                    const view = this.childViews[i];
                    view.Top = 0;
                    view.Left = width - view.Width;
                    if (view.Height > this.height) {
                        view.Height = this.height;
                    }
                    width -= view.Width + this.gap;
                }
                if (this.childViews.length > 0) {
                    width -= this.gap;
                }
                this.width = width + this.paddingRight;
                this.reSize();
            } else if (this.alignX == "center") {
                let width = (this.width - expWidth) / 2;
                for (let i = 0; i < this.childViews.length; i++) {
                    const view = this.childViews[i];
                    view.Top = 0;
                    view.Left = width;
                    if (view.Height > this.height) {
                        view.Height = this.height;
                    }
                    width += view.Width + this.gap;
                }
                if (this.childViews.length > 0) {
                    width -= this.gap;
                }
            }
        }
    }

    public remove(view: QUIView, destroy?: boolean): void {
        super.remove(view, destroy);
        this.reLayout();
    }

    add(child: QUIView) {
        super.add(child);
        this.reLayout();
        return this;
    }
}
